/*
 * QTXpost - a Qt-based blog client.
 * Copyright (C) 2010 Helen Sommer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (in COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 *
 * The project is hosted on http://code.google.com/p/qtxpost
 */

#ifndef SERVER_H
#define SERVER_H

#include <QtCore>
#include <QtGui>
#include <QtSql>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

#include "xmlrpcclient.h"
#include "httpclient.h"

#include "ui_Server.h"
#include "Communities.h"

enum LJ_OPERATIONS {
  LJ_NONE,
  LJ_UPDATE_LOGIN,
  LJ_UPDATE_RESPONSE,
  LJ_POST_LOGIN,
  LJ_POST_RESPONSE,
  LJ_GET_LAST_POST_LOGIN,
  LJ_GET_LAST_POST_RESPONSE,
  LJ_GET_LAST_ITEM_LOGIN,
  LJ_GET_LAST_ITEM_RESPONSE,
  LJ_GET_LIST_POSTS_LOGIN,
  LJ_GET_LIST_POSTS_RESPONSE,
  LJ_GET_POST_LOGIN,
  LJ_FIND_ITEM_LOGIN,
  LJ_FIND_ITEM_RESPONSE,
  LJ_GET_TRUST_GROUPS_LOGIN,
  LJ_GET_TRUST_GROUPS_RESPONSE,
  LJ_GET_FRIENDS_LOGIN,
  LJ_GET_FRIENDS_RESPONSE,
  LJ_UPDATE_GROUPS_LOGIN,
  LJ_UPDATE_GROUPS_RESPONSE,
  LJ_UPDATE_FRIENDS_LOGIN,
  LJ_UPDATE_FRIENDS_RESPONSE,
  LJ_GET_CIRCLE_LOGIN,
  LJ_GET_CIRCLE_RESPONSE,
  LJ_UPDATE_CIRCLE_LOGIN,
  LJ_UPDATE_CIRCLE_RESPONSE,
};

enum UPDATE_MODES {
  MODE_ADD_GROUP,
  MODE_UPDATE_GROUP,
  MODE_DELETE_GROUP,
  MODE_ADD_USER_GROUP,
  MODE_DELETE_USER_GROUP,
  MODE_UPDATE_USER,
};

enum SERVER_TYPES {
  NO_TYPE = -1,
  LJ_TYPE = 0,
  DW_TYPE = 1
};

enum USER_IDENTITIES {
  IDENTITY_USER,
  IDENTITY_COMMUNITY,
  IDENTITY_SYNDICATED,
  IDENTITY_NEWS,
  IDENTITY_SHARED
};

class Server : public QFrame, private Ui::Server
{
  Q_OBJECT

    public:
  Server(int count, QWidget *parent = 0);
  Server(QString username, QString password, QString label, QString server, int serverType, QWidget *parent = 0);

  int id;
  int position;
  QString username, password, server, label;
  bool hidden, deleted, selected, copied;
  int itemid;
  QList<QString> moods;
  Communities *communities;
  QString lastSubject;
  QDateTime lastDate;
  bool inSync;
  int serverType;
  QString updateGroupName;
  int updateGroupId;
  int updateGroupType; // 0: trustgroup; 1: content filter
  qulonglong updateGroupMask;
  int updateGroupPublic, updateGroupSortOrder;
  QString updateUserName;
  QString updateForeground, updateBackground;
  int updateMode;

  void renameCommunitiesButton(QString title);
  void deleteServer();
  void hideServer(bool doHide);
  void startPost();
  void startGetLastPost();
  void startGetLastItem();
  void startGetListPosts();
  void startGetPost();
  void startFindItem();
  void startGetTrustGroups();
  void startGetWatchGroups();
  void startGetFriends();
  void startGetCircle();
  void startUpdateGroups();
  void startUpdateFriends();
  void startUpdateCircle();

  bool eventInSync(QString subject, QDateTime date, QString &message);
  int getCommunities();
  int getCommunities(QString community);
  QCheckBox *getSelected();
  void unselect();
  void updatePosition(int position);

  public slots:
  void update(QString username,QString password, QString site, QString server, int serverType, bool updateIcons, bool forceUpdate, bool updateMoods);
  void selectServer(int selected);
  void changeIcon(QString keyword);
  void setIconKeyword(QString keyword);
  void disableCopyIconKeyword();

  private slots:
  void clientResponse(QVariant response);
  void changeIcon();
  void downloadFinished();
  void selectCommunities();
  void copyIconKeyword(int doCopy);

  void finished(QNetworkReply *reply);
  void downloadStart();

 private:
  XmlRpcClient *client;
  QNetworkAccessManager *manager;
  int state;
  int downloadCount, downloadTarget;
  QString defaultIconUrl;
  bool doUpdateCommunities, doUpdateGroups;
  bool doUpdateIcons, doForceUpdateIcons;
  bool doUpdateMoods;
  QStringList communityNames;
  int currentCommunity;
  QVector<QString> download;
  QDateTime now;
  QString postSignature;
  QHash <QString, int> identityTypes;
      
  void initialize();
  void initializeRpc();
  QString escapeSql(QString string);
  void updateDatabase(QString username, QString password, QString label, QString server, int serverType);
  void startGetUserData();
  void updateIcons();
  void updateMoods();
  void updateMoods(QHash<int,QString> moods);
  void updateCommunities(QList<QString> communities);
  void updateGroups(QList<QMap < QString, QVariant> > groups, int type);
  void updateIcons(QList<QString> iconUrls, QList<QString> iconKeywords);
  void updateFriends(QHash<QString, QMap < QString, QVariant> >  friends, QMultiHash<QString, QString> flags);
  void updateGroup();
  void updateUser();
  void addUser(QString userName, QString fullName);
  void addUserDw(QString userName, QString fullName, int trust, int watch, int trusted, int watched);
  void addContentFilter(QString groupName, int bit);
  void addUserContentFilter(QString userName, int bit);
  void deleteUserContentFilter(QString userName, int bit);      
  QByteArray cleanupEvent(QByteArray text);

  QString authResponse(QString challenge);
  void getUserDataRequest(QString challenge);
  void getUserDataResponse(QVariantMap result);
  void postRequest(QString challenge, QString usejournal);
  void postResponse(QVariantMap result);
  void getLastPostRequest(QString challenge, QString usejournal);
  void getLastPostResponse(QVariantMap result);
  void getLastItemRequest(QString challenge, QString usejournal);
  void getLastItemResponse(QVariantMap result);
  void getListPostsRequest(QString challenge, QString usejournal);
  void getListPostsResponse(QVariantMap result);
  void findItemRequest(QString challenge, QString usejournal);
  void findItemResponse(QVariantMap result);
  void getTrustGroupsRequest(QString challenge);
  void getTrustGroupsResponse(QVariantMap result);
  void getFriendsRequest(QString challenge);
  void getFriendsResponse(QVariantMap result);
  void getCircleRequest(QString challenge);
  void getCircleResponse(QVariantMap result);
  void updateGroupsRequest(QString challenge);
  void updateGroupsResponse(QVariantMap result);
  void updateFriendsRequest(QString challenge);
  void updateFriendsResponse(QVariantMap result);
  void updateCircleRequest(QString challenge);
  void updateCircleResponse(QVariantMap result);
};

#endif
